package com.nageoffer.shortlink.admin.common.context;

import com.nageoffer.shortlink.admin.pojo.entity.UserInfo;

import java.util.Optional;

public class UserContext {
    private static final ThreadLocal<UserInfo> userThreadLocal = new ThreadLocal<>();

    public static void setUser(UserInfo user){
        userThreadLocal.set(user);
    }

    public static Long getUserId(){
        UserInfo userInfo = userThreadLocal.get();
        return Optional.ofNullable(userInfo).map(UserInfo::getUserId).orElse(null);
    }

    public static String getUsername(){
        UserInfo userInfo = userThreadLocal.get();
        return Optional.ofNullable(userInfo).map(UserInfo::getUsername).orElse(null);
    }
    public static String getUserRealName(){
        UserInfo userInfo = userThreadLocal.get();
        return Optional.ofNullable(userInfo).map(UserInfo::getRealName).orElse(null);
    }

    public static void removeUser(){
        userThreadLocal.remove();
    }
}
